package com.question.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.question.Constant.BaseConstant;
import com.question.Exception.BaseException;
import com.question.common.PageResult;
import com.question.entity.Paper;
import com.question.entity.Question;
import com.question.entity.User;
import com.question.mapper.PaperMapper;
import com.question.mapper.QuestionMapper;
import com.question.mapper.UserMapper;
import com.question.service.PaperService;
import com.question.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements PaperService {

    @Resource
    private PaperMapper paperMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private RedisTemplate redisTemplate;

    public void add(String name, String description, String cover) {

        LambdaQueryWrapper<Paper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getName, name);
        Paper paper = this.getOne(lambdaQueryWrapper);
        if (paper != null) {
            throw new BaseException("该问卷已存在");
        }
        Paper paper1 = new Paper();
        paper1.setCover(cover);
        paper1.setCreatetime(new Date());
        paper1.setDescription(description);
        paper1.setName(name);
        paper1.setUsecount(BaseConstant.ZERO);
        paper1.setStatus(BaseConstant.ZERO);
        this.save(paper1);
    }

    public PageResult getByPage(Integer pageNo, Integer pageSize, String keyword) {
        PageResult pageResult = new PageResult();
        IPage<Paper> page = new Page<>(pageNo, pageSize);
        IPage<Paper> list = paperMapper.selectByPage(page, keyword);
        pageResult.setRecords(list.getRecords());
        pageResult.setTotal(list.getTotal());
        return pageResult;
    }

    public void edit(Paper paper) {
        LambdaQueryWrapper<Paper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getName, paper.getName());
        lambdaQueryWrapper.notIn(Paper::getId, paper.getId());
        Paper paper1 = paperMapper.selectOne(lambdaQueryWrapper);
        if (paper1 != null) {
            throw new BaseException("该问卷已存在,请重新命名");
        }
        paperMapper.updateById(paper);
    }

    public void delete(Integer id) {
        LambdaQueryWrapper<Paper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getId, id);
        Paper paper = paperMapper.selectOne(lambdaQueryWrapper);
        if (paper == null) {
            throw new BaseException("该问卷不存在");
        }
        if (paper.getStatus().equals(1)) {
            throw new BaseException("发布中的问卷不能删除");
        }
        paperMapper.deleteById(id);

    }

    public void publish(Integer id) {
        LambdaQueryWrapper<Paper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getId, id);
        Paper paper = paperMapper.selectOne(lambdaQueryWrapper);
        if (paper == null) {
            throw new BaseException("该问卷不存在");
        }
        paper.setStatus(paper.getStatus() == 0 ? 1 : 0);
        paperMapper.updateById(paper);
    }
    public  Paper getdetail(Integer id){
        LambdaQueryWrapper<Paper> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getId,id);
        Paper paper=paperMapper.selectOne(lambdaQueryWrapper);
        if(paper == null){
            throw new BaseException("该问卷不存在");
        }
        List<Question> list=questionMapper.selectDetail(id);
        paper.setQuestionList(list);
        new Thread(()->{
            increamentPaper(id);
        }).start();
        return paper;

    }

    private void increamentPaper(Integer id) {
 redisTemplate.opsForZSet().incrementScore(BaseConstant.PAPER,id,1);
   }

}
